/* Moral, Mert, and Efe Tokdemir 
�Justices `en Garde'_Ideological Determinants of the Dissolution of Anti-Establishment Parties� 
International Poltiical Science Review
Replication File
10.26.2015 */

** DATA OPERATIONS
* Generating Left-Right scores for Justices
cd "/Users/mmoral/Dropbox/SUNY Binghamton PhD/Miscellaneous/Constitutional Court/Replication"
import excel "Judicial Review and Party Dissolution Cases.xlsx", sheet("Judicial Review Cases-Code") firstrow clear
keep jid jname c2011_179 c2011_178 c2011_171 c2011_104 c2011_103 c2011_96 c2011_95 c2011_94 c2011_90 c2011_86 c2011_85 c2011_81 c2011_80 c2011_78 c2011_77 c2011_76 c2011_75 c2011_74 c2011_66 c2011_65 c2011_64 c2011_59 c2011_54 c2011_46 c2011_39 c2011_27 c2011_13 c2010_121 c2010_120 c2010_119 c2010_87 c2010_85 c2010_76 c2010_56 c2010_50 c2010_49 c2010_44 c2010_43 c2010_26 c2010_16 c2010_12 c2009_153 c2009_152 c2009_151 c2009_149 c2009_148 c2009_147 c2009_145 c2009_141 c2009_112 c2009_99 c2009_92 c2009_85 c2009_84 c2009_69 c2009_68 c2009_48 c2009_38 c2009_7 c2009_6 c2009_5 c2008_185 c2008_184 c2008_183 c2008_165 c2008_158 c2008_157 c2008_153 c2008_150 c2008_148 c2008_146 c2008_119 c2008_116 c2008_113 c2008_108 c2008_107 c2008_106 c2008_97 c2008_96 c2008_79 c2008_75 c2008_63 c2008_48 c2008_47 c2008_46 c2008_40 c2007_91 c2007_86 c2007_83 c2007_66 c2007_56 c2007_54 c2007_53 c2007_48 c2007_45 c2007_35 c2007_31 c2007_27 c2007_12 c2007_7 c2007_6 c2007_4 c2006_120 c2006_108 c2006_92 c2006_66 c2006_40 c2006_4 c2006_3 c2006_2 c2005_111 c2005_110 c2005_108 c2005_104 c2005_103 c2005_96 c2005_93 c2005_73 c2005_72 c2005_59 c2005_35 c2005_33 c2005_25 c2005_14 c2005_10 c2005_8 c2004_124 c2004_123 c2004_84 c2004_6 c2004_4 c2003_91 c2003_88 c2003_87 c2003_86 c2003_76 c2003_38 c2009_91 c2009_90 c1992_42 c1992_32 c1992_20 c1992_6 c1992_5 c1991_50 c1991_43 c1991_29 c1991_21 c1991_20 c1991_17 c1991_15 c1991_8 c1991_7 c1991_1 c1990_33 c1990_30 c1990_26 c1990_24 c1990_21 c1990_17 c1990_10 c1990_1 c1990_8 c1990_3 c1990_2 c1989_48 c1989_42 c1989_36 c1989_29 c1989_23 c1989_10 c1989_7 c1988_55 c1988_32 c1988_25 c1988_23 c1988_19 c1988_18 c1988_8 c1988_3 c1988_2 c1988_1 c1987_27 c1987_19 c1987_14 c1987_11 c1987_3 c1987_1 c1986_26 c1986_4 c1985_16 c1985_7 c1985_6 c1985_4 c1985_1 c1984_2 c1989_12 c1988_9 c1987_13 c1987_12 c1986_28 c1985_8 c2007_82 c2005_3 c2004_13 c2003_100 c2002_69 c2002_59 c2002_58 c2002_57 c2002_40 c2002_29 c2002_19 c2001_357 c2001_67 c2001_46 c1998_18 c1999_5 c1997_53 c1996_45 c1995_64 c1995_63 c1995_58 c1995_46 c1995_45 c1995_44 c1995_42 c1995_26 c1995_12 c1994_80 c1994_79 c1994_53 c1994_52 c1994_51 c1994_50 c1994_49 c1994_48 c1994_442 c1993_54 c1993_53 c1993_52 c1993_51 c1993_50 c1993_49 c1993_48 c1993_47 c1993_46 c1993_45 c1993_39 c1993_38 c1993_37 c1993_36 c1993_35 c1993_34 c1993_33 c1993_32 c1993_28 c1993_23 c1993_19 c1993_18 c1992_30 c1994_24 c1994_23 c2002_94 c2002_24 c1995_59 c2005_91 c2004_14 c2003_104 c2003_72 c2003_63 c2003_3 c2002_192 c2002_98 c2002_56 c2002_46 c2002_28 c2002_25 c2002_20 c2002_16 c2001_356 c2001_354 c2001_90 c2001_63 c2001_56 c2001_41 c2000_46 c2000_45 c2000_44 c2000_43 c2000_42 c2000_41 c2000_40 c2000_39 c2000_38 c2000_37 c2000_36 c2000_35 c2000_34 c2000_33 c2000_32 c2000_31 c2000_30 c2000_29 c2000_27 c2000_23 c2000_21 c2000_17 c2000_4 c1999_41 c1999_39 c1999_37 c1999_36 c1998_65 c2009_102 c2009_15 c2007_68 c2007_44 c2007_33 c2007_18 c2007_5 c2007_3 c2004_33 c2011_88 c1994_28 c1994_27 c1994_26 c1994_25 c1994_31 c1994_30 c1994_29 c1994_33 c1994_32 c1994_36 c1994_35 c1994_34 c1994_37 c1994_41 c1994_40 c1994_39 c1994_38 c1994_68 c1997_66 c1996_44 c1996_35 c1995_60 c1998_52 c1996_15 c1996_14 c1996_13 c1994_78 c1997_73 c1997_74 c1998_50 c2006_27 c2005_99 c2004_105 c2004_101 c2003_92 c2002_91 c2002_51 c2002_21 c2006_112 c2006_57 c2004_109 c2002_201 c1999_49 c1999_6 c1999_1 c1998_6 c1997_69 c1997_58 c1997_52 c1997_40 c1997_33 c1997_3 c1996_47 c1996_43 c1996_36 c1996_7 c1995_23 c1994_652 c1994_452 c1994_422 c1993_402

* Appellants from Center-left Parties
foreach var of varlist c2011_179 c2011_178 c2011_171 c2011_104 c2011_103 c2011_96 c2011_95 c2011_94 c2011_90 c2011_86 c2011_85 c2011_81 c2011_80 c2011_78 c2011_77 c2011_76 c2011_75 c2011_74 c2011_66 c2011_65 c2011_64 c2011_59 c2011_54 c2011_46 c2011_39 c2011_27 c2011_13 c2010_121 c2010_120 c2010_119 c2010_87 c2010_85 c2010_76 c2010_56 c2010_50 c2010_49 c2010_44 c2010_43 c2010_26 c2010_16 c2010_12 c2009_153 c2009_152 c2009_151 c2009_149 c2009_148 c2009_147 c2009_145 c2009_141 c2009_112 c2009_99 c2009_92 c2009_85 c2009_84 c2009_69 c2009_68 c2009_48 c2009_38 c2009_7 c2009_6 c2009_5 c2008_185 c2008_184 c2008_183 c2008_165 c2008_158 c2008_157 c2008_153 c2008_150 c2008_148 c2008_146 c2008_119 c2008_116 c2008_113 c2008_108 c2008_107 c2008_106 c2008_97 c2008_96 c2008_79 c2008_75 c2008_63 c2008_48 c2008_47 c2008_46 c2008_40 c2007_91 c2007_86 c2007_83 c2007_66 c2007_56 c2007_54 c2007_53 c2007_48 c2007_45 c2007_35 c2007_31 c2007_27 c2007_12 c2007_7 c2007_6 c2007_4 c2006_120 c2006_108 c2006_92 c2006_66 c2006_40 c2006_4 c2006_3 c2006_2 c2005_111 c2005_110 c2005_108 c2005_104 c2005_103 c2005_96 c2005_93 c2005_73 c2005_72 c2005_59 c2005_35 c2005_33 c2005_25 c2005_14 c2005_10 c2005_8 c2004_124 c2004_123 c2004_84 c2004_6 c2004_4 c2003_91 c2003_88 c2003_87 c2003_86 c2003_76 c2003_38 c2009_91 c2009_90 c1992_42 c1992_32 c1992_20 c1992_6 c1992_5 c1991_50 c1991_43 c1991_29 c1991_21 c1991_20 c1991_17 c1991_15 c1991_8 c1991_7 c1991_1 c1990_33 c1990_30 c1990_26 c1990_24 c1990_21 c1990_17 c1990_10 c1990_1 c1990_8 c1990_3 c1990_2 c1989_48 c1989_42 c1989_36 c1989_29 c1989_23 c1989_10 c1989_7 c1988_55 c1988_32 c1988_25 c1988_23 c1988_19 c1988_18 c1988_8 c1988_3 c1988_2 c1988_1 c1987_27 c1987_19 c1987_14 c1987_11 c1987_3 c1987_1 c1986_26 c1986_4 c1985_16 c1985_7 c1985_6 c1985_4 c1985_1 c1984_2 c2002_94 c2002_24 c1995_59 c1999_1 c1998_6 c1997_69 c1997_58 c1997_52 c1997_40 c1997_33 c1997_3 c1996_47 c1996_43 c1996_36 c1996_7 c1995_23 c1994_652 c1994_452 c1994_422 c1993_402 {
gen parl`var'=`var'
recode parl`var' (1=-1) (0=1)
}

* Appellants from Center-right, Far right, Nationalist Parties
foreach var of varlist c2007_82 c2005_3 c2004_13 c2003_100 c2002_69 c2002_59 c2002_58 c2002_57 c2002_40 c2002_29 c2002_19 c2001_357 c2001_67 c2001_46 c1998_18 c1999_5 c1997_53 c1996_45 c1995_64 c1995_63 c1995_58 c1995_46 c1995_45 c1995_44 c1995_42 c1995_26 c1995_12 c1994_80 c1994_79 c1994_53 c1994_52 c1994_51 c1994_50 c1994_49 c1994_48 c1994_442 c1993_54 c1993_53 c1993_52 c1993_51 c1993_50 c1993_49 c1993_48 c1993_47 c1993_46 c1993_45 c1993_39 c1993_38 c1993_37 c1993_36 c1993_35 c1993_34 c1993_33 c1993_32 c1993_28 c1993_23 c1993_19 c2005_91 c2004_14 c2003_104 c2003_72 c2003_63 c2003_3 c2002_192 c2002_98 c2002_56 c2002_46 c2002_28 c2002_25 c2002_20 c2002_16 c2001_356 c2001_354 c2001_90 c2001_63 c2001_56 c2001_41 c2000_46 c2000_45 c2000_44 c2000_43 c2000_42 c2000_41 c2000_40 c2000_39 c2000_38 c2000_37 c2000_36 c2000_35 c2000_34 c2000_33 c2000_32 c2000_31 c2000_30 c2000_29 c2000_27 c2000_23 c2000_21 c2000_17 c2000_4 c1999_41 c1999_39 c1999_37 c1999_36 c1998_65 c1998_52 c1996_15 c1996_14 c1996_13 c1994_78 c2002_201 c1999_49 c1999_6 {
gen parr`var'=`var'
recode parr`var' (0=-1)
}

* Left-right Score Based on the Decisions in Cases where Parliamentary Parties w/ Identifiable Ideologies as Appellants
egen float sclr_parl = rowmean(parr* parl*)
drop par*
save "Ideologyv1.dta", replace

**For wnominate in R (All Judicial Review Cases)
* Data Operations (Justice ID and Appointing Presidents)
import excel "To Stata.xlsx", sheet("Justices") firstrow clear
save "Justicev1.dta", replace
keep jid presap
save "Justicev2.dta", replace

import excel "Judicial Review and Party Dissolution Cases.xlsx", sheet("Judicial Review Cases-Code") firstrow clear
merge m:1 jid using "Justicev2.dta"
drop _merge jid
order presap, before(c1982_3)
recode c* (missing=-99)
outsheet using "Judicial Review All.csv", comma nonames replace

/* R Code for Two-Dimensional Wnominate Scores 
library(pscl)
library(wnominate)
library(foreign)
library(gdata)

## All Cases ##
Court <- read.csv("/Users/mmoral/Dropbox/SUNY Binghamton PhD/Miscellaneous/Constitutional Court/Replication/Judicial Review All.csv",header=FALSE,strip.white=TRUE)
Courtnames<- Court[,1]
legData <- matrix(Court[,2],length(Court[,2]),1)
colnames(legData) <- "party"
Court <- Court[,-c(1,2)]

# Y�lmaz Aliefendio?lu and Ha?im K�l�� determine the polarity
rc <- rollcall(Court, 
               yea=c(1), 
               nay=c(0),
               missing=c(-99),
	       legis.names=Courtnames,
               legis.data=legData,
               desc="Constitutional Court of Turkey 1983-2011")
result <- wnominate(rc, dims=2, polarity=c(57,85))
plot(result)
summary(result)
result$legislators
result$fits
write.fwf(x=format(as.data.frame(result$legislators),digits=5,width=10, scientific=FALSE),"/Users/mmoral/Dropbox/SUNY Binghamton PhD/Miscellaneous/Constitutional Court/Replication/wnominate.csv") */

***MERGING DIFFERENT IDEOLOGY SCORES
clear
import delimited "wnominate.csv", delimiter(comma) varnames(1) 
gen jid=_n
destring coord1d, generate(coord1d_jr) force
destring coord2d, generate(coord2d_jr) force
keep jid coord1d_jr coord2d_jr
save "wnominate.dta", replace

use "Ideologyv1.dta", clear
merge m:1 jid using "wnominate.dta"
drop _merge jname
save "Ideologyv2.dta", replace

* Voting Records
import excel "To Stata.xlsx", sheet("Dissolution Cases") firstrow clear
foreach var of varlist c1983_1 c1983_2 c1983_3 c1983_4 c1983_5 c1984_1 c1988_1 c1989_1 c1991_1 c1991_2 c1991_3 c1992_1 c1993_1 c1993_2 c1993_3 c1994_1 c1994_2 c1994_3 c1995_1 c1996_1 c1997_1 c1997_2 c1997_3 c1998_1 c1999_1 c2001_2 c2003_1 c2004_1 c2004_2 c2004_3 c2004_4 c2004_5 c2004_6 c2004_7 c2008_1 c2009_1 c2009_2 c2009_3 c2009_4 c2011_1 {
rename `var' `var'a
}
save "Votev1.dta", replace

foreach var of varlist _all {
rename `var' pop_`var'
} 
rename pop_jid jid
rename pop_jname jname
reshape long pop_,i(jid jname) string
rename _j case
rename pop_ vote
drop if missing(vote)
gen caseid=substr(case,2,6)
save "Votev2.dta", replace

* Justice Data
import excel "To Stata.xlsx", sheet("Justices") firstrow clear
save "Justicev1.dta", replace

* Case Data
import excel "To Stata.xlsx", sheet("Cases") firstrow clear
save "Casev1.dta", replace

* Merging Datasets
use "Votev2.dta", clear
merge m:1 jid using "Justicev1.dta"
drop if _merge!=3
drop _merge
merge m:1 caseid using "Casev1.dta"
drop if _merge!=3
drop _merge

* Inverse Coding the Vote Records Based on the Outcome of the Case
recode vote (1=0) (0=1) if case=="c1983_4a" | case=="c1983_4b" | case=="c1983_5a" | case=="c1983_5b" | case=="c1988_1a" | case=="c1989_1a" | case=="c1991_3a" | case=="c1996_1a" | case=="c1997_3a" | case=="c1999_1b" | case=="c1999_1c" | case=="c2001_2a" | case=="c2004_1a" | case=="c2004_2a" | case=="c2004_3a" | case=="c2004_4a" | case=="c2004_5a" | case=="c2005_6a" | case=="c2004_7a" | case=="c2009_1a" | case=="c2009_2a" | case=="c2009_3a"
save "Dissolutionv1.dta", replace

* Controls
gen year1=substr(caseid,1,4)
destring year1, generate(year)
drop year1
gen experience=year-entry
recode relig ethnic soc pfamily (missing=0)
egen presapn=group(presap)   
recode presapn (4=0) (2=0) (1=2) (5=1) (6=3) (3=4)
save "Dissolutionv2.dta", replace

** Importing Ideology Scores
use "Dissolutionv2.dta", clear
merge m:1 jid using "Ideologyv2.dta"
drop if missing(jname)
drop if missing(coord1d_jr)
drop _merge
replace parl2=1 if parl2>0

* Variable Labels
lab var vote "Vote for Dissolution"
lab var sclr_parl "Mean Left-Right Score"
lab var coord1d_jr "1st Dimension (W-nominate) "
lab var coord2d_jr "2nd Dimension (W-nominate)"
lab var position "Status of the Justice"
lab var experience "Years of Experience"
lab var education "Education Level"
lab var jid "Justice ID"
lab var party "Party in the Case"
lab var jname "Justice Name"
lab var case "Case ID"
lab var procedural "Procedural Case"
lab var date "Date of the Case"
lab var year "Year of the Case"
lab var entry "Year of Appointment"
lab var pfamily "Party Family"
lab var parlbalance "Parliament's Ideology"
lab var parl2 "Parliamentary Party"
lab var presapn "Appointing President"

keep case party jid jname date year entry experience position education pfamily parlbalance parl2 vote sclr_parl coord1d_jr coord2d_jr procedural presapn
order case party jid jname date year entry experience position education pfamily parlbalance parl2 vote sclr_parl coord1d_jr coord2d_jr procedural presapn
sort case jname 
save "Replication Data.dta", replace
